package com.jxmcloud.business.cut.service.impl.app.activity;

import cn.hutool.core.bean.BeanUtil;
import com.jxmcloud.business.cut.constants.CutErrorMsg;
import com.jxmcloud.business.cut.dto.client.GetOrderByProcessDto;
import com.jxmcloud.business.cut.dtoCommon.OrderProcess;
import com.jxmcloud.business.cut.service.app.activity.IUserOrderTranService;
import com.jxmcloud.business.cut.service.user.IUserOrderService;
import com.jxmcloud.business.cut.vo.UserOrderTranVo;
import com.jxmcloud.business.cut.vo.client.GetOrderByProcessVo;
import com.jxmcloud.business.cut.vo.json.ConfigProgressVo;
import com.jxmcloud.business.cut.vo.user.PlayerInfoVo;
import com.jxmcloud.business.cut.vo.user.UserOrderListVo;
import com.xlcloud.platform.api.exception.BusinessException;
import lombok.extern.slf4j.Slf4j;
import org.springframework.context.annotation.Lazy;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Objects;

@Slf4j
@Service
public class UserOrderTranServiceImpl implements IUserOrderTranService {
    @Resource
    private com.jxmcloud.business.cut.service.user.IUserBagService userBagService;
    @Resource
    @Lazy  // 添加延迟加载注解
    private IUserOrderService userOrderService;

    //
    @Override
    public GetOrderByProcessVo getOrderByProcess(GetOrderByProcessDto dto, PlayerInfoVo player) {
        String error = CutErrorMsg.ERROR_FALSE;
        //定义返回信息
        GetOrderByProcessVo result = new GetOrderByProcessVo();
        do {
            //验证流程
            UserOrderListVo userOrderListVo = player.getUserOrderListVo();
            if (Objects.isNull(userOrderListVo)) {
                log.error("用户订单列表为空");
                break;
            }
            Integer progressId = dto.getProgressId();
            ConfigProgressVo progressConfig = userOrderService.checkProgressDto(progressId);
            if (Objects.isNull(progressConfig)) {
                log.error("流程配置为空");
                break;
            }

            //返回用户的订单相关信息
            UserOrderTranVo userOrderTranVo = userOrderService.updateUserOrders(player,progressId);
            if (Objects.isNull(userOrderTranVo)) {
                log.error("用户订单为空");
                break;
            }
//
//            //奖励
//            Integer[] intsMy = progressConfig.getAward();
//            //添加到背包
//            userBagService.addItem(player, intsMy[0], intsMy[1]);

            OrderProcess order = BeanUtil.copyProperties(userOrderTranVo, OrderProcess.class);
            result.setOrder(order);

            error = CutErrorMsg.ERROR_TRUE;
        } while (false);
        if (!error.equals(CutErrorMsg.ERROR_TRUE)) {
            // 抛出异常
            throw new BusinessException(error);
        }
        return result;
    }
}